package com.bbs.toutiao.mapper.clinet;


import com.bbs.toutiao.vo.article.ArticleVo;
import com.bbs.toutiao.vo.client.UserVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ClientUserEditorMapper {


    //写入用户历史记录
    @Insert("insert into t_user_history(user_id,article_id,created_at) values(#{userId},#{articleId},#{createdAt})")
    int insertHistory(int userId, int articleId, String createdAt);


    //查询用户历史记录列表的文章，降序排序
    @Select("select id as h_id,article_id, DATE(created_at) as time " +
            "from t_user_history where user_id = #{userId} " +
            "ORDER BY created_at DESC limit #{page} , #{size}")
    List<Map<String, Object>> selectHistoryListId(int userId, int page, int size);

    //统计历史记录总数
    @Select("select count(*) from t_user_history where user_id = #{userId}")
    long selectHistoryListCount(int userId);

    //查询用户历史记录
    List<ArticleVo> selectHistory(List<Integer> articlesId, int userId);


    //修改背景图片
    @Update("update t_users set image = #{image} where id = #{id}")
    int updateUserBackground(int id, String image);

    //往关注表中添加关注的用户
    @Insert("insert into t_follows (follower_id,following_id) values (#{userId},#{followId})")
    int insertFollows(int userId, int followId);

    //取消关注
    @Delete("delete from  t_follows where follower_id = #{userId} and following_id = #{followId}")
    int deleteFollows(int userId, int followId);

    //收藏
    @Insert("insert into t_favorites (user_id,article_id) values (#{userId},#{articleId})")
    int insertFavorites(int userId, int articleId);

    //取消收藏
    @Delete("delete from  t_favorites where user_id = #{userId} and article_id = #{articleId}")
    int deleteFavorites(int userId, int articleId);

    //查询用户详细信息,根据id
    @Select("SELECT u.*" +
            ", (SELECT COUNT(*) FROM t_follows f WHERE f.follower_id = u.id) as follow_count " +
            ", (SELECT COUNT(*) FROM t_follows f2 WHERE f2.following_id = u.id) as following_count " +
            ", (SELECT COUNT(*) FROM t_favorites c WHERE c.user_id = u.id) as collections_count " +
            "FROM t_users as u " +
            "WHERE u.id = #{id}")
    UserVo getUserInfoByUserId(Integer id);

    //批量删除历史记录
    int deleteHistories(Integer userId,List<Integer> listId);
}
